PHP オブジェクト指向プログラミング  例題一覧

PHP Object-Oriented Programming

手続き型プログラミングは、 データに対して操作を実行するプロシージャまたは関数を作成することに関するものであり、オブジェクト指向プログラミング(OOP)は、データと関数の両方を含むオブジェクトを作成することに関するものです。オブジェクト指向プログラミングには、手続き型プログラミングよりもいくつかの利点があります。
    OOPは、
  • より速く、より簡単に実行できます。
  • プログラムに明確な構造を提供します。
  • コードを DRY に保ち、 "Don't Repeat Yourself" を維持するのに役立ち、コードの保守、変更、およびデバッグを容易にします。「 "Don't Repeat Yourself" (DRY)」 原則とは、コードの繰り返しを減らすことです。 アプリケーションに共通するコードを抽出し、それらを 1 つの場所に配置して、繰り返すのではなく再利用する必要があります。
  • より少ないコードと短い開発時間で完全に再利用可能なアプリケーションを作成できます。

Constructor

A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Destructor

As constructors and destructors helps reducing the amount of code, they are very useful!

Access Modifiers

Properties and methods can have access modifiers which control where they can be accessed.

    There are three access modifiers:
  1. public - the property or method can be accessed from everywhere. This is default
  2. protected- the property or method can be accessed within the class and by classes derived from that class
  3. private - the property or method can ONLY be accessed within the class

Inheritance

Inheritance in OOP = When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.

Overriding Inherited Methods

Inherited methods can be overridden by redefining the methods (use the same name) in the child class.

The final Keyword

The final keyword can be used to prevent class inheritance or to prevent method overriding.

Constants

Constants cannot be changed once it is declared. Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

Abstract Classes

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. Interfaces are declared with the interface keyword:

interface

Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism". Interfaces are declared with the interface keyword:

Traits

PHP only supports single inheritance: a child class can inherit only from one single parent. Traits are used to declare methods that can be used in multiple classes. Traits are declared with the trait keyword:

Static Methods

Static methods can be called directly - without creating an instance of the class first.

Static Properties

Static properties can be called directly - without creating an instance of a class.

Namespaces
    Namespaces are qualifiers that solve two different problems:
  1. They allow for better organization by grouping classes that work together to perform a task
  2. They allow the same name to be used for more than one class
MySQL
    MySQL is the most popular database system used with PHP. PHP combined with MySQL are cross-platform such as Windows and serve on a Unix/Linux platform
Iterables

An iterable is any value which can be looped through with a foreach() loop.

Creating Iterables

All arrays are iterables, so any array can be used as an argument of a function that requires an iterable. Iterators: Any object that implements the Iterator interface can be used as an argument of a function that requires an iterable.

プログラムポートフォリオ (Program Portfolio)

PHP OOP